//
//  HomeFeedCell.m
//  XiaoHongShu
//
//  Created by Jason on 2022/6/18.
//

#import "HomeFeedCell.h"
#import "ItemFrame.h"
#import "MainDataModel.h"

@interface HomeFeedCell ()
@property (nonatomic, weak) UIImageView *imgView;
@property (nonatomic, weak) UILabel *textLabel;
@property (nonatomic, weak) UIImageView *userImgView;  //用户头像
@property (nonatomic, weak) UILabel *userNameLabel;  //用户名
@property (nonatomic, weak) UIButton *likeBtn;
@property (nonatomic, weak) UILabel *adsLabel;  //广告赞助
@property (nonatomic, weak) UIImageView *vedioImgView;  //视频头像

@end

@implementation HomeFeedCell

-(void)setItemFrame:(ItemFrame *)itemFrame {
    _itemFrame = itemFrame;
    
    //设置数据
    [self setupData];
    
    //设置frame
    [self setupFrame];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


//纯代码创建控件，需要重写
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        //创建控件
        [self setUpUI];
    }
    return self;
}

#pragma mark - setUpUI
- (void)setUpUI {
    //简单创建 imgView
    //item显示图片
    UIImageView *imgView = [[UIImageView alloc]init];
    //_imgView.image = [UIImage imageNamed:@""];
    // imgView.backgroundColor = kRedColor;
    [self.contentView addSubview:imgView];
    _imgView = imgView;
    
    //视频头像
    UIImageView *vedioImgView = [[UIImageView alloc]init];
   // vedioImgView.backgroundColor = [UIColor yellowColor];
    vedioImgView.image = [UIImage imageNamed:@"24gf-playCircle"];
    [self.contentView addSubview:vedioImgView];
    _vedioImgView = vedioImgView;
    
    //是否赞助label
    UILabel *adsLabel = [[UILabel alloc]init];
    adsLabel.backgroundColor = kLightGrayColor;
//    adsLabel.alpha = 0.5;
    adsLabel.text = @"赞助";
    adsLabel.font = kFont(9);
    adsLabel.textColor = kWhiteColor;
    adsLabel.textAlignment = NSTextAlignmentCenter;
    adsLabel.layer.cornerRadius = 3;
    adsLabel.layer.masksToBounds = YES;
    [self.contentView addSubview:adsLabel];
    _adsLabel = adsLabel;
    
    
    //正文
    UILabel *textLabel = [[UILabel alloc]init];
   // textLabel.text = @"不愧被央视点名…巨巨巨好吃！最近一直想吃烤鱼，刚好晚上就刷到了网友做的这个空气炸锅版烤鲳鱼！我愿称之为蕞加吃法！[赞R]\n绝了，真的不输外面的烤鱼店！[哇R]出锅秒空！";
    textLabel.font = [UIFont systemFontOfSize:13.0];
    textLabel.numberOfLines = 2;
    if (@available(iOS 13.0, *)) {
        textLabel.textColor = [UIColor labelColor];
    } else {
        textLabel.textColor = kBlackColor;
    }
    textLabel.backgroundColor = kClearColor;
    [self.contentView addSubview:textLabel];
    _textLabel = textLabel;
    
    //用户头像
    UIImageView *userImgView = [[UIImageView alloc]init];
    userImgView.backgroundColor = kRedColor;
    userImgView.layer.cornerRadius = 10;
    userImgView.layer.masksToBounds = YES;
    [self.contentView addSubview:userImgView];
    _userImgView = userImgView;
    
   //用户名
    UILabel *userNameLabel = [[UILabel alloc]init];
    userNameLabel.font = [UIFont systemFontOfSize:11.0];
    userNameLabel.textAlignment = NSTextAlignmentLeft;
    userNameLabel.textColor = kLightGrayColor;
    [self.contentView addSubview:userNameLabel];
    _userNameLabel = userNameLabel;
    
    
    //用户点赞数
    UIButton *likeBtn = [[UIButton alloc]init];
    likeBtn.titleLabel.font = [UIFont systemFontOfSize:11.0];
    likeBtn.backgroundColor = kClearColor;
    //设置按钮文字居右(无效)
   // likeBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    //设置按钮文字颜色
    [likeBtn setTitleColor:kLightGrayColor forState:UIControlStateNormal];
    //设置按钮整体居右
    likeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    likeBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
    likeBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 5);
    [self.contentView addSubview:likeBtn];
    _likeBtn = likeBtn;
}


#pragma mark - 设置frame
- (void)setupFrame {
    //item 图片
    _imgView.frame = self.itemFrame.imgViewFrame;
    
    //视频头像
    _vedioImgView.frame = self.itemFrame.vedioImgViewFrame;
    
    //是否赞助
    _adsLabel.frame = self.itemFrame.adsLabelFrame;
    
    //正文
    _textLabel.frame = self.itemFrame.textLabelFrame;
    
    //头像
    _userImgView.frame = self.itemFrame.userImgViewFrame;
    
    //用户名
    _userNameLabel.frame = self.itemFrame.userNameLabelFrame;
    
    //用户点赞数
    _likeBtn.frame = self.itemFrame.likeBtnFrame;
}


#pragma mark - 设置数据
- (void)setupData {
    //获取整个模型数据
    MainDataModel *mainDataModel = self.itemFrame.mainDataModel;
    
    //获取图片模型数据
    //NSLog(@"imageCount = %ld",mainDataModel.images.count);
    for (ImagesModel *imagesModel in mainDataModel.images) {
      //  NSLog(@"imagesModel = %@",imagesModel);
        [_imgView sd_setImageWithURL:[NSURL URLWithString:imagesModel.url]];
    }
    
    //获取视频头像
   // NSLog(@"视频count = %@", mainDataModel.video);
    if ([mainDataModel.type isEqualToString:@"normal"]) {
        _vedioImgView.hidden = YES;
    } else {
        _vedioImgView.hidden = NO;
    }
    
    //获取赞助文本
  //  NSLog(@"isAds = %ld",(long)mainDataModel.isAds);
    if (mainDataModel.isAds) {
        _adsLabel.hidden = NO;
    } else {
        _adsLabel.hidden = YES;
    }
   
    //获取正文
    _textLabel.text = [NSString stringWithFormat:@"%@ %@", mainDataModel.name, mainDataModel.textDescription];
    
    //获取头像
    [_userImgView sd_setImageWithURL:[NSURL URLWithString:mainDataModel.userImage]];
    
    //获取用户名
    _userNameLabel.text = mainDataModel.userName;
    
    //获取用户点赞数
    if (mainDataModel.likeCount >= 10000) {
        [_likeBtn setTitle:[NSString stringWithFormat:@"%ld 万",mainDataModel.likeCount / 10000] forState:UIControlStateNormal];
    } else {
        [_likeBtn setTitle:[NSString stringWithFormat:@"%ld",mainDataModel.likeCount] forState:UIControlStateNormal];
    }
    if (mainDataModel.inLike) {
        [_likeBtn setImage:[UIImage imageNamed:@"shixin"] forState:UIControlStateHighlighted];
    } else {
        [_likeBtn setImage:[UIImage imageNamed:@"kongxin"] forState:UIControlStateNormal];
    }
}

- (void)layoutSubviews {
   // NSLog(@"homeFeedCellFrame = %@", NSStringFromCGRect(self.frame));
}


@end
